<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>InitPHP框架 PHP框架 - A PHP Framework - DB API</title>
<link href="static/common.css" type="text/css" rel="stylesheet" />
<meta name="keywords" content="php框架，国产php框架, initphp框架，MVC，分层体系" />
<meta name="description" content="initphp框架是一款国产php框架。initphp框架主要基于MVC模式，具备代码清晰，操作简单，功能齐全，开发快速，高效安全等特点，是您选择php框架的首选。" />
</head>
<body>
<div class="header">
  <div class="header_nav">InitPHP (A PHP Framework) 用户手册</div>
  <div class="header_div">
    <div class="header_title">第6章 6.5.2 DB API</div>
    <div class="header_right"><a href="6_5_1.htm">上一页</a> <a href="6_5_3.htm">下一页</a> </div>
  </div>
</div>
<div class="title">目录</div>
<ul style="list-style:none;">
  <li><a href="#$this->dao->db->affected_rows">$this->dao->db->affected_rows</a></li>
  <li><a href="#$this->dao->db->close">$this->dao->db->close</a></li>
  <li><a href="#$this->dao->db->error">$this->dao->db->error</a></li>
  <li><a href="#$this->dao->db->fetch_assoc">$this->dao->db->fetch_assoc</a></li>
  <li><a href="#$this->dao->db->fetch_fields">$this->dao->db->fetch_fields</a></li>
  <li><a href="#$this->dao->db->fmod_identify">$this->dao->db->fmod_identify</a></li>
  <li><a href="#$this->dao->db->free_result">$this->dao->db->free_result</a></li>
  <li><a href="#$this->dao->db->init_db">$this->dao->db->init_db</a></li>
  <li><a href="#$this->dao->db->free_result">$this->dao->db->free_result</a></li>
  <li><a href="#$this->dao->db->insert_id">$this->dao->db->insert_id</a></li>
  <li><a href="#$this->dao->db->month_identify">$this->dao->db->month_identify</a></li>
  <li><a href="#$this->dao->db->num_fields">$this->dao->db->num_fields</a></li>
  <li><a href="#$this->dao->db->num_identify">$this->dao->db->num_identify</a></li>
  <li><a href="#$this->dao->db->num_rows">$this->dao->db->num_rows</a></li>
  <li><a href="#$this->dao->db->query">$this->dao->db->query</a></li>
  <li><a href="#$this->dao->db->result">$this->dao->db->result</a></li>
  <li><a href="#$this->dao->db->transaction_commit">$this->dao->db->transaction_commit</a></li>
  <li><a href="#$this->dao->db->transaction_rollback">$this->dao->db->transaction_rollback</a></li>
  <li><a href="#$this->dao->db->transaction_start">$this->dao->db->transaction_start</a></li>
</ul>
<div class="nav"></div>


<div class="title">1. $this->dao->db->affected_rows() 前一次操作影响的记录数<a name="$this->dao->db->affected_rows"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p> 前一次操作影响的记录数。在Dao中使用</p>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class testaDao extends Dao {

	public function test() {
		$sql = sprintf("SELECT * FROM %s %s ORDER BY %s %s %s", $table_name, $where, $id_key, $sort, $limit);
		$result = $this->dao->db->query($sql);
		$this->dao->db->affected_rows();
	}
}
</pre>
<div class="nav"></div>

<div class="title">2. $this->dao->db->close() 关闭连接<a name="$this->dao->db->close"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p>  关闭连接。在Dao中使用</p>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class testaDao extends Dao {

	public function test() {
		$sql = sprintf("SELECT * FROM %s %s ORDER BY %s %s %s", $table_name, $where, $id_key, $sort, $limit);
		$result = $this->dao->db->query($sql);
		$this->db->close();
	}
}
</pre>
<div class="nav"></div>

<div class="title">3. $this->dao->db->error()  错误信息<a name="$this->dao->db->error"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p>  错误信息。在Dao中使用</p>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class testaDao extends Dao {

	public function test() {
		$sql = sprintf("SELECT * FROM %s %s ORDER BY %s %s %s", $table_name, $where, $id_key, $sort, $limit);
		$result = $this->dao->db->query($sql);
		$this->db->error();
	}
}
</pre>
<div class="nav"></div>

<div class="title">4. $this->dao->db->fetch_assoc($result) 从结果集中取得一行作为关联数组<a name="$this->dao->db->fetch_assoc"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p> 从结果集中取得一行作为关联数组。在Dao中使用</p>
<div class="title">
  <h3>参数：</h3>
</div>
<table>
  <tr>
    <th style="width:20%;"> 参数 </th>
    <th style="width:20%;"> 类型 </th>
    <th style="width:10%;"> 是否必须 </th>
    <th style="width:50%;"> 描述 </th>
  </tr>
    <tr>
      <td>$result</td>
      <td>Obj </td>
      <td>是</td>
      <td>结果集对象</td>
    </tr>
</table>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class testaDao extends Dao {

	public function test() {
		$sql = sprintf("SELECT * FROM %s %s ORDER BY %s %s %s", $table_name, $where, $id_key, $sort, $limit);
		$result = $this->dao->db->query($sql);
		if (!$result) return false;
		$temp = array();
		while ($row = $this->dao->db->fetch_assoc($result)) {
			$temp[] = $row;
		}
	}
}
</pre>
<div class="nav"></div>

<div class="title">5. $this->dao->db->fetch_fields($result) 从结果集中取得列信息并作为对象返回<a name="$this->dao->db->fetch_fields"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p>  从结果集中取得列信息并作为对。在Dao中使用</p>
<div class="title">
  <h3>参数：</h3>
</div>
<table>
  <tr>
    <th style="width:20%;"> 参数 </th>
    <th style="width:20%;"> 类型 </th>
    <th style="width:10%;"> 是否必须 </th>
    <th style="width:50%;"> 描述 </th>
  </tr>
    <tr>
      <td>$result</td>
      <td>Obj </td>
      <td>是</td>
      <td>结果集对象</td>
    </tr>
</table>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class testaDao extends Dao {

	public function test() {
		$sql = sprintf("SELECT * FROM %s %s ORDER BY %s %s %s", $table_name, $where, $id_key, $sort, $limit);
		$result = $this->dao->db->query($sql);
		$this->db->fetch_fields($result);
	}
}
</pre>
<div class="nav"></div>

<div class="title">6. $this->dao->db->fmod_identify($num, $tbl, $default = 1) 求余数的方式获取分表-分库方法<a name="$this->dao->db->fmod_identify"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p>根据数值来进行分表，一般情况下，可以根据UID来进行分表方法。自定义余数,例如：$default = 7 就是除以7来求余。在Dao中使用</p>
<div class="title">
  <h3>参数：</h3>
</div>
<table>
  <tr>
    <th style="width:20%;"> 参数 </th>
    <th style="width:20%;"> 类型 </th>
    <th style="width:10%;"> 是否必须 </th>
    <th style="width:50%;"> 描述 </th>
  </tr>
    <tr>
      <td>$num</td>
      <td>Number </td>
      <td>是</td>
      <td>数值</td>
    </tr>
	    <tr>
      <td>$tbl </td>
      <td>String </td>
      <td>是</td>
      <td>模板前缀</td>
    </tr>
		    <tr>
      <td>$default </td>
      <td>Int</td>
      <td>否</td>
      <td>求余数的数字</td>
    </tr>
</table>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class testaDao extends Dao {
	
	public $db = 'test';
	public function test() {
		echo $this->dao->db->fmod_identify(2343204, 'pw', $default = 7);
	}
}
</pre>
<div class="nav"></div>

<div class="title">7. $this->dao->db->free_result($result)  释放结果内存<a name="$this->dao->db->fmod_identify"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p>  释放结果内存。在Dao中使用</p>
<div class="title">
  <h3>参数：</h3>
</div>
<table>
  <tr>
    <th style="width:20%;"> 参数 </th>
    <th style="width:20%;"> 类型 </th>
    <th style="width:10%;"> 是否必须 </th>
    <th style="width:50%;"> 描述 </th>
  </tr>
    <tr>
      <td>$result</td>
      <td>Obj </td>
      <td>是</td>
      <td>结果集对象</td>
    </tr>
</table>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class testaDao extends Dao {

	public function test() {
		$sql = sprintf("SELECT * FROM %s %s ORDER BY %s %s %s", $table_name, $where, $id_key, $sort, $limit);
		$result = $this->dao->db->query($sql);
		$this->db->free_result($result);
	}
}
</pre>
<div class="nav"></div>

<div class="title">8. $this->dao->db->init_db($db = '') 初始化DB链接<a name="$this->dao->db->init_db"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p>该函数用来初始化DB，可以通过修改$db参数来初始化多数据库链接。一般情况下用户不需要使用该函数，InitPHP框架里面已经做掉了这部分功能。在Dao中使用</p>
<div class="title">
  <h3>参数：</h3>
</div>
<table>
  <tr>
    <th style="width:20%;"> 参数 </th>
    <th style="width:20%;"> 类型 </th>
    <th style="width:10%;"> 是否必须 </th>
    <th style="width:50%;"> 描述 </th>
  </tr>
    <tr>
      <td>$db</td>
      <td>String </td>
      <td>否</td>
      <td>需要链接到DB </td>
    </tr>
</table>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
$this->dao->db->init_db($db = '')
</pre>
<div class="nav"></div>

<div class="title">9. $this->dao->db->insert_id()  获取上一INSERT的ID值<a name="$this->dao->db->insert_id"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p>   获取上一INSERT的ID值。在Dao中使用</p>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class testaDao extends Dao {

	public function test() {
		$sql = sprintf("SELECT * FROM %s %s ORDER BY %s %s %s", $table_name, $where, $id_key, $sort, $limit);
		$result = $this->dao->db->query($sql);
		$this->db->insert_id();
	}
}
</pre>
<div class="nav"></div>

<div class="title">10. $this->dao->db->month_identify($tbl, $defaultId = '') 按月分表-分库方法 <a name="$this->dao->db->month_identify"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p>该一般在分表分库到时候，可以用这个函数来计算分表分库的方法。在Dao中使用</p>
<div class="title">
  <h3>参数：</h3>
</div>
<table>
  <tr>
    <th style="width:20%;"> 参数 </th>
    <th style="width:20%;"> 类型 </th>
    <th style="width:10%;"> 是否必须 </th>
    <th style="width:50%;"> 描述 </th>
  </tr>
    <tr>
      <td>$tbl</td>
      <td>String </td>
      <td>是</td>
      <td>表-库前缀</td>
    </tr>
	    <tr>
      <td>$defaultId </td>
      <td>String </td>
      <td>否</td>
      <td>默认的表月份</td>
    </tr>
</table>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class testaDao extends Dao {
	
	public $db = 'test';
	public function test() {
		echo $this->dao->db->month_identify('pw');
	}
}
</pre>
<div class="nav"></div>

<div class="title">11. $this->dao->db->num_fields($result)  结果集中的字段数量<a name="$this->dao->db->num_fields"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p> 结果集中的字段数量。在Dao中使用</p>
<div class="title">
  <h3>参数：</h3>
</div>
<table>
  <tr>
    <th style="width:20%;"> 参数 </th>
    <th style="width:20%;"> 类型 </th>
    <th style="width:10%;"> 是否必须 </th>
    <th style="width:50%;"> 描述 </th>
  </tr>
    <tr>
      <td>$result</td>
      <td>Obj </td>
      <td>是</td>
      <td>结果集对象</td>
    </tr>
</table>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class testaDao extends Dao {

	public function test() {
		$sql = sprintf("SELECT * FROM %s %s ORDER BY %s %s %s", $table_name, $where, $id_key, $sort, $limit);
		$result = $this->dao->db->query($sql);
		$this->db->num_fields($result);
	}
}
</pre>
<div class="nav"></div>

<div class="title">12. $this->dao->db->num_identify($num, $tbl, $default = 1) 根据数值来确定分表-分库方法<a name="$this->dao->db->num_identify"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p>根据数值来进行分表，一般情况下，可以根据UID来进行分表方法。也可以自定义截取需要获取数值的位数，例如2位:00,01,02,03。在Dao中使用</p>
<div class="title">
  <h3>参数：</h3>
</div>
<table>
  <tr>
    <th style="width:20%;"> 参数 </th>
    <th style="width:20%;"> 类型 </th>
    <th style="width:10%;"> 是否必须 </th>
    <th style="width:50%;"> 描述 </th>
  </tr>
   <tr>
      <td>$num</td>
      <td>Number </td>
      <td>是</td>
      <td>数值</td>
    </tr>
	    <tr>
      <td>$tbl </td>
      <td>String </td>
      <td>是</td>
      <td>模板前缀</td>
    </tr>
		    <tr>
      <td>$default </td>
      <td>Int</td>
      <td>否</td>
      <td>默认截取长度</td>
    </tr>
</table>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class testaDao extends Dao {
	
	public $db = 'test';
	public function test() {
		echo $this->dao->db->num_identify(2343204, 'pw', $default = 2);
	}
}
</pre>
<div class="nav"></div>

<div class="title">13. $this->dao->db->num_rows($result)  结果集中的行数<a name="$this->dao->db->num_rows"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p>结果集中的行数。在Dao中使用</p>
<div class="title">
  <h3>参数：</h3>
</div>
<table>
  <tr>
    <th style="width:20%;"> 参数 </th>
    <th style="width:20%;"> 类型 </th>
    <th style="width:10%;"> 是否必须 </th>
    <th style="width:50%;"> 描述 </th>
  </tr>
    <tr>
      <td>$result</td>
      <td>Obj </td>
      <td>是</td>
      <td>结果集对象</td>
    </tr>
</table>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class testaDao extends Dao {

	public function test() {
		$sql = sprintf("SELECT * FROM %s %s ORDER BY %s %s %s", $table_name, $where, $id_key, $sort, $limit);
		$result = $this->dao->db->query($sql);
		$this->db->num_rows($result);
	}
}
</pre>
<div class="nav"></div>

<div class="title">14. $this->dao->db->query($sql) 执行SQL语句<a name="$this->dao->db->query"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p>  执行SQL语句。在Dao中使用</p>
<div class="title">
  <h3>参数：</h3>
</div>
<table>
  <tr>
    <th style="width:20%;"> 参数 </th>
    <th style="width:20%;"> 类型 </th>
    <th style="width:10%;"> 是否必须 </th>
    <th style="width:50%;"> 描述 </th>
  </tr>
    <tr>
      <td>$sql</td>
      <td>String </td>
      <td>是</td>
      <td>数据库语句</td>
    </tr>
</table>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class testaDao extends Dao {

	public function test() {
		$this->dao->db->query('SELECT * FROM table');
	}
}
</pre>
<div class="nav"></div>

<div class="title">15. $this->dao->db->result($result, $num=1) 结果集中的行数<a name="$this->dao->db->result"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p> 结果集中的行数。在Dao中使用</p>
<div class="title">
  <h3>参数：</h3>
</div>
<table>
  <tr>
    <th style="width:20%;"> 参数 </th>
    <th style="width:20%;"> 类型 </th>
    <th style="width:10%;"> 是否必须 </th>
    <th style="width:50%;"> 描述 </th>
  </tr>
    <tr>
      <td>$result</td>
      <td>Obj </td>
      <td>是</td>
      <td>结果集对象</td>
    </tr>
    <tr>
      <td>$num</td>
      <td>Int </td>
      <td>是</td>
      <td>条数</td>
    </tr>
</table>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class testaDao extends Dao {

	public function test() {
		$this->dao->db->result($result, $num=1);
	}
}
</pre>
<div class="nav"></div>

<div class="title">16. $this->dao->db->transaction_commit() 事务提交<a name="$this->dao->db->transaction_commit"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p>提交事务。在Dao中使用</p>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class testaDao extends Dao {

	public function test() {
		$this->dao->db->transaction_start();
		$r1 = $this->dao->db->query("select * from user where uid = 1");
		$r2 = $this->dao->db->query("delete from user where uid = 1");
		if ($r1 && $r2) {
			$this->dao->db->transaction_commit();
		} else {
			$this->dao->db->transaction_rollback();
		}
	}
}
</pre>
<div class="nav"></div>

<div class="title">17. $this->dao->db->transaction_rollback() 事务回滚<a name="$this->dao->db->transaction_rollback"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p>回滚事务。在Dao中使用</p>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class testaDao extends Dao {

	public function test() {
		$this->dao->db->transaction_start();
		$r1 = $this->dao->db->query("select * from user where uid = 1");
		$r2 = $this->dao->db->query("delete from user where uid = 1");
		if ($r1 && $r2) {
			$this->dao->db->transaction_commit();
		} else {
			$this->dao->db->transaction_rollback();
		}
	}
}
</pre>
<div class="nav"></div>

<div class="title">18. $this->dao->db->transaction_start() 事务开始<a name="$this->dao->db->transaction_start"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p>开始事务。在Dao中使用</p>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class testaDao extends Dao {

	public function test() {
		$this->dao->db->transaction_start();
		$r1 = $this->dao->db->query("select * from user where uid = 1");
		$r2 = $this->dao->db->query("delete from user where uid = 1");
		if ($r1 && $r2) {
			$this->dao->db->transaction_commit();
		} else {
			$this->dao->db->transaction_rollback();
		}
	}
}
</pre>
<div class="nav"></div>

<div class="footer">版权所有：<a href="">http://initphp.com</a> InitPHP (A PHP Framework) By @Aliyun_zhuli</div>
<script src="static/jquery.js" type="text/javascript"></script>
<link href="static/prettify/prettify.css" rel="stylesheet" type="text/css">
<script src="static/prettify/prettify.js" type="text/javascript"></script>
<script type="text/javascript" src="static/comm.js"></script>
</body>
</html>
